﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;
using Microsoft.Practices.Composite.Presentation.Commands;
using System.Windows;
using Common.Model;

namespace Common.UI.EM.Agregation
{
    public class GenericAgregationPresenter<E>
        where E : IObjectWithChangeTracker, new()
    {

        public AgregationView<E> View { get; set; }


        E _newEntity;
        /// <summary>
        /// A usar como binding
        /// </summary>
        public E NewEntity
        {
            get { return _newEntity; }
            set { _newEntity = value; }
        }


        public DelegateCommand<Object> AddCommand
        {
            get;
            set;
        }

        public DelegateCommand<Object> DeleteCommand
        {
            get;
            set;
        }

        public IObjectWithChangeTracker SelectedEntity
        {
            get
            {
                return this.View.GetSelectedEntity();
            }
        }

        public GenericAgregationPresenter(AgregationView<E> view)
        {
            this.View = view;

            this.AddCommands();

            this.InitializePresenter();
        }

        private void InitializePresenter()
        {
            NewEntity = new E();
        }


        private void AddCommands()
        {
            this.AddCommand = new DelegateCommand<object>(ExecuteAdd, CanExecuteAdd);
            this.DeleteCommand = new DelegateCommand<object>(ExecuteDelete, CanExecuteDelete);
        }

        private void ExecuteDelete(Object arg)
        {
            if (this.SelectedEntity != null && this.ValidateOnDelete() && this.View.ValidateDeleteEntity((E)this.SelectedEntity))
            {
                this.DeleteEntity();
            }
        }

        protected virtual bool ValidateOnDelete()
        {
            if (MessageBox.Show("¿Está seguro que desea eliminar permanentemente el registro?", "Confirmación", MessageBoxButton.YesNo) == MessageBoxResult.No)
            {
                return false;
            }
            return true;
        }

        private Boolean CanExecuteDelete(Object arg)
        {
            return true;
        }

        private void DeleteEntity()
        {
            this.View.Remove((E)this.SelectedEntity);
        }


        private void ExecuteAdd(Object arg)
        {
            if (this.View.ValidateNewEntity(this.NewEntity))
            {
                this.AddEntity();
            }
        }

        private Boolean CanExecuteAdd(Object arg)
        {
            return true;
        }

        private void AddEntity()
        {
            if (this.View.List == null)
            {
                this.View.List = new TrackableCollection<E>();
            }
            this.View.Add(this.NewEntity);

            this.NewEntity = new E();

            this.View.UpdateListView();
        }
    }
}
